﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Security.Principal;

namespace Freshome.d
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class Multint : IMultint
    {
        //
        // Majoraw operations（主料）
        //
        public List<b.Majoraw> GetMajoraws(bool usedOnly)
        {
            MajorawDog dog = new MajorawDog();
            return Editool.Read<List<b.Majoraw>>(() =>
            {
                return dog.GetMajoraws(usedOnly);
            });
        }
        private int SaveMajoraw(b.Majoraw entity, MajorawDog dog, WindowsPrincipal wp)
        {
            int rt = 0;
            Editool.Save<b.Majoraw>(arg =>
            {
                rt = dog.Saventity(entity);
            }, entity, wp);
            return rt;
        }
        public Dictionary<short, string> SaveMajoraws(b.Majoraw[] entities)
        {
            Dictionary<short, string> rts = new Dictionary<short, string>();
            MajorawDog dog = new MajorawDog();
            var wp = Editool.GetWindowsPrincipal();
            foreach (var entity in entities)
            {
                try
                {
                    if (this.SaveMajoraw(entity, dog, wp) > 0) rts.Add(entity.Mid, entity.Stamp);
                }
                catch (FaultException ex)
                {
                    rts.Add(entity.Mid, ex.Message);  // 新增记录entity.Mid的传入值为从short.MinValue开始的负值
                }
            }
            return rts;
        }
        public bool DeleMajoraw(short mid)
        {
            MajorawDog dog = new MajorawDog();
            return Editool.Dele<short>(arg =>
            {
                return dog.Delete(arg) > 0;
            }, mid, dog.Getype().Name);
        }
        //
        // Minoraw operations（佐料）
        //
        public List<b.Minoraw> GetMinoraws(bool usedOnly)
        {
            MinorawDog dog = new MinorawDog();
            return Editool.Read<List<b.Minoraw>>(() =>
            {
                return dog.GetMinoraws(usedOnly);
            });
        }
        private int SaveMinoraw(b.Minoraw entity, MinorawDog dog, WindowsPrincipal wp)
        {
            int rt = 0;
            Editool.Save<b.Minoraw>(arg =>
            {
                rt = dog.Saventity(entity);
            }, entity, wp);
            return rt;
        }
        public Dictionary<short, string> SaveMinoraws(b.Minoraw[] entities)
        {
            Dictionary<short, string> rts = new Dictionary<short, string>();
            MinorawDog dog = new MinorawDog();
            var wp = Editool.GetWindowsPrincipal();
            foreach (var entity in entities)
            {
                try
                {
                    if (this.SaveMinoraw(entity, dog, wp) > 0) rts.Add(entity.Mid, entity.Stamp);
                }
                catch (FaultException ex)
                {
                    rts.Add(entity.Mid, ex.Message);    // 新增记录entity.Mid的传入值为从short.MinValue开始的负值
                }
            }
            return rts;
        }
        public bool DeleMinoraw(short mid)
        {
            MinorawDog dog = new MinorawDog();
            return Editool.Dele<short>(arg =>
            {
                return dog.Delete(arg) > 0;
            }, mid, dog.Getype().Name);
        }
        //
        // Pairaw operations（主料搭配）
        //
        public List<b.Pairaw> GetPairaws(short mid)
        {
            PairawDog dog = new PairawDog();
            return Editool.Read<List<b.Pairaw>>(() =>
            {
                return dog.GetEntities(e => e.Jmx == mid).ToList();
            });
        }
        private int SavePairaw(b.Pairaw entity, PairawDog dog, WindowsPrincipal wp)
        {
            int rt = 0;
            Editool.Save<b.Pairaw>(arg =>
            {
                rt = dog.Saventity(entity);
            }, entity, wp);
            return rt;
        }
        public Dictionary<short, string> SavePairaws(b.Pairaw[] entities)
        {
            Dictionary<short, string> rts = new Dictionary<short, string>();
            PairawDog dog = new PairawDog();
            var wp = Editool.GetWindowsPrincipal();
            foreach (var entity in entities)
            {
                try
                {
                    if (this.SavePairaw(entity, dog, wp) > 0) rts.Add(entity.Mid, entity.Stamp);
                }
                catch (FaultException ex)
                {
                    rts.Add(entity.Mid, ex.Message);
                }
            }
            return rts;
        }
        public bool DelePairaw(short mid)
        {
            PairawDog dog = new PairawDog();
            return Editool.Dele<short>(arg =>
            {
                return dog.Delete(arg) > 0;
            }, mid, dog.Getype().Name);
        }
        //
        // Healthold operations（食疗科目）
        //
        public List<b.Healthold> GetHealtholds(bool withChildren)
        {
            HealtholdDog dog = new HealtholdDog();
            return Editool.Read<List<b.Healthold>>(() =>
            {
                return dog.GetHealtholds(withChildren).ToList();
            });
        }
        private int SaveHealthold(b.Healthold entity, HealtholdDog dog, WindowsPrincipal wp)
        {
            int rt = 0;
            Editool.Save<b.Healthold>(arg =>
            {
                rt = dog.Saventity(entity);
            }, entity, wp);
            return rt;
        }
        public Dictionary<short, string> SaveHealtholds(b.Healthold[] entities)
        {
            Dictionary<short, string> rts = new Dictionary<short, string>();
            HealtholdDog dog = new HealtholdDog();
            var wp = Editool.GetWindowsPrincipal();
            foreach (var entity in entities)
            {
                try
                {
                    if (this.SaveHealthold(entity, dog, wp) > 0) rts.Add(entity.Mid, entity.Stamp);
                }
                catch (FaultException ex)
                {
                    rts.Add(entity.Mid, ex.Message);
                }
            }
            return rts;
        }
        public bool DeleHealthold(short mid)
        {
            HealtholdDog dog = new HealtholdDog();
            return Editool.Dele<short>(arg =>
            {
                return dog.Delete(arg) > 0;
            }, mid, dog.Getype().Name);
        }
        public Dictionary<short, string> ShortMajoraws()
        {
            MajorawDog dog = new MajorawDog();
            return Editool.Read<Dictionary<short, string>>(() =>
            {
                return dog.AllEntities().ToDictionary(m => m.Mid, m => m.Name);
            });
        }
        //
        // Rawithealth operations（食疗主料）
        //
        private int SaveRawithealth(b.Rawithealth entity, RawithealthDog dog, WindowsPrincipal wp)
        {
            int rt = 0;
            Editool.Save<b.Rawithealth>(arg =>
            {
                rt = dog.Saventity(entity);
            }, entity, wp);
            return rt;
        }
        public Dictionary<short, string> SaveRawithealthes(b.Rawithealth[] entities)
        {
            Dictionary<short, string> rts = new Dictionary<short, string>();
            RawithealthDog dog = new RawithealthDog();
            var wp = Editool.GetWindowsPrincipal();
            foreach (var entity in entities)
            {
                try
                {
                    if (this.SaveRawithealth(entity, dog, wp) > 0) rts.Add(entity.Mid, entity.Stamp);
                }
                catch (FaultException ex)
                {
                    rts.Add(entity.Mid, ex.Message);
                }
            }
            return rts;
        }
        public bool DeleRawithealth(short mid)
        {
            RawithealthDog dog = new RawithealthDog();
            return Editool.Dele<short>(arg =>
            {
                return dog.Delete(arg) > 0;
            }, mid, dog.Getype().Name);
        }
        //
        // Recipe operations（食谱）
        //
        public b.Recipe GetRecipe(short mid, bool withCousin)
        {
            RecipeDog dog = new RecipeDog();
            return Editool.Read<b.Recipe>(() =>
            {
                return dog.GetRecipe(mid, withCousin);
            });
        }
        public List<b.Recipe> GetRecipes(b.RecipeCateg ct, bool withChildren)
        {
            RecipeDog dog = new RecipeDog();
            return Editool.Read<List<b.Recipe>>(() =>
            {
                return dog.GetRecipes(ct, withChildren);
            });
        }
        private int SaveRecipe(b.Recipe entity, RecipeDog dog, WindowsPrincipal wp)
        {
            int rt = 0;
            Editool.Save<b.Recipe>(arg =>
            {
                rt = dog.Saventity(arg);
            }, entity, wp);
            return rt;
        }
        public Dictionary<short, string> SaveRecipes(b.Recipe[] entities)
        {
            Dictionary<short, string> rts = new Dictionary<short, string>();
            RecipeDog dog = new RecipeDog();
            var wp = Editool.GetWindowsPrincipal();
            foreach (var entity in entities)
            {
                try
                {
                    if (this.SaveRecipe(entity, dog, wp) > 0) rts.Add(entity.Mid, entity.Stamp);
                }
                catch (FaultException ex)
                {
                    rts.Add(entity.Mid, ex.Message);
                }
            }
            return rts;
        }
        public bool DeleRecipe(short mid)
        {
            RecipeDog dog = new RecipeDog();
            return Editool.Dele<short>(arg =>
            {
                return dog.Delete(arg) > 0;
            }, mid, dog.Getype().Name);
        }
        //
        // Steplan operations（菜谱制作）
        //
        private int SaveSteplan(b.Steplan entity, SteplanDog dog, WindowsPrincipal wp)
        {
            int rt = 0;
            Editool.Save<b.Steplan>(arg =>
            {
                rt = dog.Saventity(arg);
            }, entity, wp);
            return rt;
        }
        public Dictionary<short, string> SaveSteplans(b.Steplan[] entities)
        {
            Dictionary<short, string> rts = new Dictionary<short, string>();
            SteplanDog dog = new SteplanDog();
            var wp = Editool.GetWindowsPrincipal();
            foreach (var entity in entities)
            {
                try
                {
                    if (this.SaveSteplan(entity, dog, wp) > 0) rts.Add(entity.Mid, entity.Stamp);
                }
                catch (FaultException ex)
                {
                    rts.Add(entity.Mid, ex.Message);    // 新增记录entity.Mid的传入值为从int.MinValue开始的负值
                }
            }
            return rts;
        }
        public bool DeleSteplan(short mid)
        {
            SteplanDog dog = new SteplanDog();
            return Editool.Dele<short>(arg =>
            {
                return dog.Delete(arg) > 0;
            }, mid, dog.Getype().Name);
        }
        //
        // Majorecipe operations（配主料）
        //
        private int SaveMajorecipe(b.Majorecipe entity, MajorecipeDog dog, WindowsPrincipal wp)
        {
            int rt = 0;
            Editool.Save<b.Majorecipe>(arg =>
            {
                rt = dog.Saventity(arg);
            }, entity, wp);
            return rt;
        }
        public Dictionary<short, string> SaveMajorecipes(b.Majorecipe[] entities)
        {
            Dictionary<short, string> rts = new Dictionary<short, string>();
            MajorecipeDog dog = new MajorecipeDog();
            var wp = Editool.GetWindowsPrincipal();
            foreach (var entity in entities)
            {
                try
                {
                    if (this.SaveMajorecipe(entity, dog, wp) > 0) rts.Add(entity.Mid, entity.Stamp);
                }
                catch (FaultException ex)
                {
                    rts.Add(entity.Mid, ex.Message);
                }
            }
            return rts;
        }
        //
        // Minorecipe operations（配佐料）
        //
        private int SaveMinorecipe(b.Minorecipe entity, MinorecipeDog dog, WindowsPrincipal wp)
        {
            int rt = 0;
            Editool.Save<b.Minorecipe>(arg =>
            {
                rt = dog.Saventity(arg);
            }, entity, wp);
            return rt;
        }
        public Dictionary<short, string> SaveMinorecipes(b.Minorecipe[] entities)
        {
            Dictionary<short, string> rts = new Dictionary<short, string>();
            MinorecipeDog dog = new MinorecipeDog();
            var wp = Editool.GetWindowsPrincipal();
            foreach (var entity in entities)
            {
                try
                {
                    if (this.SaveMinorecipe(entity, dog, wp) > 0) rts.Add(entity.Mid, entity.Stamp);
                }
                catch (FaultException ex)
                {
                    rts.Add(entity.Mid, ex.Message);
                }
            }
            return rts;
        }
    }
}
